//
//  UIImageView+AD.swift
//  ADKit
//
//  Created by Adoma on 2022/4/18.
//

import RxGesture
import RxSwift

public extension UIImageView {
    
    func ad_addTarget(target: NSObject, action: Selector) {
        self.isUserInteractionEnabled = true
        let ges = UITapGestureRecognizer(target: target, action: action)
        self.addGestureRecognizer(ges)
    }
    
    func ad_invalidLayout() {
        setContentCompressionResistancePriority(.init(0), for: .vertical)
        setContentCompressionResistancePriority(.init(0), for: .horizontal)
        setContentHuggingPriority(.init(0), for: .vertical)
        setContentHuggingPriority(.init(0), for: .horizontal)
    }
    
    convenience init(imageName: String) {
        self.init(image: .init(named: imageName))
    }
    
    convenience init(contentMode: UIView.ContentMode) {
        self.init()
        self.contentMode = contentMode
        self.clipsToBounds = true
    }
    
}

extension Reactive where Base: UIImageView {

    /// by ADKit
    public var tap: Observable<Void> {
        tapGesture().when(.recognized).map({ _ in Void() })
    }
}
